package com.iknight.theory.controller;

import com.github.pagehelper.PageInfo;
import com.iknight.mgr.core.base.BaseController;
import com.iknight.mgr.core.constant.IkgConstantCode;
import com.iknight.mgr.core.utils.IkgStringUtils;
import com.iknight.mgr.core.utils.IkgWebUtils;
import com.iknight.theory.api.TeContainerService;
import com.iknight.theory.model.TEContainerModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author JeryZym
 * @date 2017/12/20
 */
@RestController
@RequestMapping("/rest/container")
@Api(value = "计量箱控制器")
public class ContainerController implements BaseController<TEContainerModel> {
    private final static Logger logger = LogManager.getLogger(ContainerController.class);

    @Autowired
    private TeContainerService<TEContainerModel, String> teContainerService;

    @Override
    public TEContainerModel format(Map<String, Object> params) {
        TEContainerModel entity = new TEContainerModel();

        entity.setContainer_id(IkgStringUtils.ObjectToString(params.get("container_id")));
        if (IkgStringUtils.isNotNull(params.get("asset_no"))) {
            entity.setAsset_no(IkgStringUtils.ObjectToString(params.get("asset_no")));
        }
        if (IkgStringUtils.isNotNull(params.get("is_effective"))) {
            entity.setIs_effective(IkgStringUtils.ObjectToString(params.get("is_effective")));
        }
        return entity;
    }


    @Override
    @PostMapping("/save")
    @ApiOperation(value = "保存方法", httpMethod = "POST")
    public Object save(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            teContainerService.save(format(params));
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        return IkgWebUtils.postIkgResponse(true, "保存成功!", IkgConstantCode.OPERSUCCESS);
    }

    @Override
    @PostMapping("/delete")
    @ApiOperation(value = "删除方法", httpMethod = "POST")
    public Object delete(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            teContainerService.delete(IkgStringUtils.ObjectToString(params.get("container_id")));
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        return IkgWebUtils.postIkgResponse(true, "删除成功!", IkgConstantCode.OPERSUCCESS);
    }

    @Override
    @PostMapping("/selectOne")
    @ApiOperation(value = "单条记录查询方法", httpMethod = "POST")
    public Object selectOne(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
//        return null;
        if (isPKeyNotNull(params)) {
            TEContainerModel entity = teContainerService.selectOne(IkgStringUtils.ObjectToString(params.get("container_id")));
            return IkgWebUtils.postIkgResponse(true, "查询成功!", entity);
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
    }

    @Override
    @PostMapping("/list")
    @ApiOperation(value = "根据参数分页查询", httpMethod = "POST")
    public Object selectByPage(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (null == params) {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        int pageIndex = IkgStringUtils.ObjectToInt(params.get("pageNumber"));
        int pageSize = IkgStringUtils.ObjectToInt(params.get("pageSize"));
        PageInfo<TEContainerModel> pageInfo = teContainerService.selectPage(params, pageIndex, pageSize);
        return IkgWebUtils.postResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public boolean isPKeyNotNull(Map<String, Object> params) {
        return null != params && IkgStringUtils.isNotNull(params.get("container_id"));
    }
}
